﻿<Serializable()> _
Public Class News

    Public Property PK_News As Integer

    Public Property Text As String

    Public Property DataInizioValidita As Date

    Public Property DataFineValidita As Date

    Public Property DataInserimento As Date

    Public Property Priorita As Integer

    Public Property Deleted As Boolean

    Public Sub New()
        PK_News = 0
    End Sub

    Public Sub LoadFromPk(objpk As Integer)

        ' Dim Evt As New Evento
        Dim sql As String = "SELECT * " & _
                            " FROM News WHERE Deleted = 0 AND PK_News = " & objpk
        Dim ds As New DataSet
        Dim dbO As New DBObject
        'Throw New Exception(sql)
        ds = dbO.FillDS(sql, Nothing, 1)
        If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            PK_News = objpk
        End If
        If Not IsDBNull(ds.Tables(0).Rows(0).Item("Text")) Then
            Text = ds.Tables(0).Rows(0).Item("Text").ToString
        End If
        If Not IsDBNull(ds.Tables(0).Rows(0).Item("DataInizioValidita")) Then
            DataInizioValidita = ds.Tables(0).Rows(0).Item("DataInizioValidita").ToString
        End If
        If Not IsDBNull(ds.Tables(0).Rows(0).Item("DataFineValidita")) Then
            DataFineValidita = ds.Tables(0).Rows(0).Item("DataFineValidita").ToString
        End If
        If Not IsDBNull(ds.Tables(0).Rows(0).Item("DataInserimento")) Then
            DataInserimento = ds.Tables(0).Rows(0).Item("DataInserimento").ToString
        End If
        If Not IsDBNull(ds.Tables(0).Rows(0).Item("Priorita")) Then
            Priorita = ds.Tables(0).Rows(0).Item("Priorita").ToString
        End If
    End Sub

    Public Function InserisciNuova() As Boolean
        Dim dbo As New DBObject
        Dim Inizio As String
        Dim Fine As String

        If DataInizioValidita.Equals(Date.MinValue) Then
            Inizio = ""
        Else
            Inizio = DataInizioValidita.ToString("yyyyMMdd")
        End If

        If DataFineValidita.Equals(Date.MinValue) Then
            Fine = ""
        Else
            Fine = DataFineValidita.ToString("yyyyMMdd")
        End If

        Dim sql As String = "INSERT INTO News (Text, Priorita, DataInizioValidita, DataFineValidita, DataInserimento, Deleted) VALUES ( " & _
                            " '" & Text.Replace("'", "''") & "'" & _
                            " ,'" & Priorita & "'" & _
                            " ,'" & Inizio & "'" & _
                            " ,'" & Fine & "'" & _
                            " ,'" & DataInserimento.ToString("yyyyMMdd") & "', 0)"

        If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then


            Return True
        Else
            Return False
        End If

    End Function

    Public Shared Function Elimina(pk_nw As Integer) As Boolean
        Dim dbo As New DBObject

        Dim sql As String = "UPDATE News SET " & _
                            " Deleted = 1 " & _
                            " WHERE PK_News = " & pk_nw

        If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

            ' Throw New Exception(sql)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Salva() As Boolean

        If PK_News = 0 Then
            InserisciNuova()
        Else
            Dim dbo As New DBObject
            Dim Inizio As String
            Dim Fine As String

            If DataInizioValidita.Equals(Date.MinValue) Then
                Inizio = ""
            Else
                Inizio = DataInizioValidita.ToString("yyyyMMdd")
            End If

            If DataFineValidita.Equals(Date.MinValue) Then
                Fine = ""
            Else
                Fine = DataFineValidita.ToString("yyyyMMdd")
            End If

            Dim sql As String = "UPDATE News SET " & _
                                " Text= '" & Text.Replace("'", "''") & "'" & _
                                " ,Priorita= '" & Priorita & "'" & _
                                " ,DataInizioValidita= '" & Inizio & "'" & _
                                " ,DataFineValidita= '" & Fine & "'" & _
                                " WHERE PK_News = " & PK_News

            If dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text) > 0 Then

                ' Throw New Exception(sql)
                Return True
            Else
                Return False
            End If

        End If

    End Function

End Class
